﻿using Newtonsoft.Json;
using Orleans;
using Pay.Admin.App_Start;
using Pay.Common.Encrypt;
using Pay.Model.Dto;
using Pay.Model.Enums;
using Pay.Service;
using Pay.Service.user;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Pay.Admin.Controllers
{
    public class UserController : Controller
    {
        /// <summary>
        /// 退出
        /// </summary>
        /// <returns></returns>
        public ActionResult SignOut()
        {
            FormsAuthentication.SignOut();
           return RedirectToAction("login");
        }
        public async Task<ActionResult> Login()
        {
            return View();
        }
        [HttpPost]
        public async Task<ActionResult> Login(LoginOutPut user)
        {
            if (user != null)
            {
                var service = GrainClient.GrainFactory.GetGrain<IUserSerive>("0");
                if (!string.IsNullOrEmpty(user.UserName) && !string.IsNullOrEmpty(user.Pwd))
                {
                    var model = await service.GetByUserName(user.UserName);
                    if (model != null)
                    {
                        if (!user.Pwd.Equals(model.Pwd))
                        {
                            return Json(new { isSuccess = false, msg = "密码错误!" });
                        }
                        if(!model.IsEnable) return Json(new { isSuccess = false, msg = "用户被禁用!" });
                        //登录成功
                        string userData = JsonConvert.SerializeObject(model);
                        var now = DateTime.Now;
                        FormsAuthenticationTicket auth = new FormsAuthenticationTicket(1, model.UserName, now, now.AddYears(1), true, userData);
                        string encTicket = FormsAuthentication.Encrypt(auth);
                        HttpCookie cookies = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                        // cookies.Expires = auth.Expiration;
                        Response.Cookies.Add(cookies);
                        return Json(new { isSuccess = true, msg = "登录成功" });
                    }
                  
                    return Json(new { isSuccess = false, msg = "用户名无效!" });
                }
                else
                {
                    return Json(new { isSuccess = false, msg = "用户名无效!" });
                }
            }
            else
            {
                return Json(new { isSuccess = false, msg = "用户名或者密码为空!" });
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ActionResult UserEdit(long userId)
        {

            return View();
        }
        /// <summary>
        /// 停用用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<JsonResult> StopUser(long userId)
        {
            var service = GrainClient.GrainFactory.GetGrain<IUserSerive>("0");
            var model = await service.GetUserById(userId);
            if (model == null)
            {
                return Json(new { success = false, msg = "用户不存在" });

            }
            else
            {
                model.IsEnable = false;
                var success = await service.Update(model);
                return Json(new { success = success, msg = "停用成功！" });
            }
        }
        /// <summary>
        /// 启用用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<JsonResult> RestartUser(long userId)
        {
            var service = GrainClient.GrainFactory.GetGrain<IUserSerive>("0");
            var model = await service.GetUserById(userId);
            if (model == null)
            {
                return Json(new { success = false, msg = "用户不存在" });

            }
            else
            {
                model.IsEnable = true;
                var success = await service.Update(model);
                return Json(new { success = success, msg = "启用成功！" });
            }
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> AddUser(UserAddModel user)
        {
            var success = false;
            var msg = "新增失败";
            if (user != null)
            {
                var service = GrainClient.GrainFactory.GetGrain<IUserSerive>("0");
               var model=await  service.GetByUserName(user.UserName);if(model!=null) return Json(new { success = false, msg = "新增失败，用户名已经存在" });
                success = await  service.Add(user);
                msg = "新增成功";
            }
            return Json(new { success = success ,msg=msg});
        }
        [HttpGet]
        public async Task<ActionResult> AddUser(RoleCategory roleType,UserType userType)
        {
            var service = GrainClient.GrainFactory.GetGrain<IRoleService>("0");
            var role=await service.GetRoleByType((int)roleType);
            ViewBag.UserType = (int)userType;
            return View(role);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>

        public async Task<ActionResult> UserDelete(long userId)
        {
            var service = GrainClient.GrainFactory.GetGrain<IUserSerive>("0");
            var success=await service.Delete(userId);
             string msg= success == true ? "删除成功" : "删除失败";
            return Json(new { success = success, msg=msg });
        }
       
    }
}